package com.zbw.weightlibrary.WeightSub;

import android.content.Context;
import com.zbw.weightlibrary.Connector.IBleControl;
import com.zbw.weightlibrary.Connector.IinfoCallback;
import java.io.File;
import OnePlusOneAndroidSDK.ScalesOS.ScalesSDK;
import OnePlusOneAndroidSDK.ScalesOS.WeightInfo;

/**
 * 作者 ：Wesley
 * 时间 ：2023-10-18 11:10
 * 这个类是干嘛的？：OnePlusOne
 */
public class OnePlusOne implements IBleControl, ScalesSDK.WeightChangedListener{
   private final IinfoCallback mCallback;
   private ScalesSDK mScalesSDK;
   protected boolean ReadWeight = false;//是否读取重量


   public OnePlusOne(Context context, IinfoCallback callback) {
      mCallback = callback;
      mScalesSDK = ScalesSDK.getInstance(context,this);
   }

   @Override
   public void bleConnect() {
      if(mScalesSDK != null){
         mScalesSDK.Open(new File("/dev/ttyS0"));
      }
   }

   @Override
   public void bleDisconnect() {
      if (mScalesSDK != null) {
         mScalesSDK.Close();
         mScalesSDK = null;
      }
   }

   @Override
   public void getWeight() {
      ReadWeight = true;
      if(mScalesSDK != null) {
         mScalesSDK.GetResult();
      }
   }

   @Override
   public void stopWeight() {
      ReadWeight = false;
   }

   @Override
   public void clearTare() {
      if(mScalesSDK != null) {
         mScalesSDK.ExitTare();
      }
   }

   @Override
   public void tare() {
      if(mScalesSDK != null) {
         mScalesSDK.Tare();
      }
   }

   @Override
   public void zero() {
      if(mScalesSDK != null) {
         mScalesSDK.Zero();
      }
   }

   @Override
   public void onWeightChanged(WeightInfo weightInfo) {
      if(ReadWeight){
         if (mCallback != null && mScalesSDK != null) {
            mCallback.onGetWeight(weightInfo.getNetWeight(),"Stable".equals(weightInfo.getStatus()));
         }
      }
   }
}
